package co.fitstart.mobile.web.controller.profile;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.wechat.web.interceptor.WechatJsConfig;

import co.fitstart.entity.user.Student;
import co.fitstart.entity.user.User;
import co.fitstart.entity.user.agent.OfflineAgent;
import co.fitstart.mobile.service.UserAgentService;
import co.fitstart.mobile.service.UserService;
import co.fitstart.mobile.web.interceptor.MobileDetect;
import co.fitstart.mobile.web.interceptor.Router;
import co.fitstart.mobile.web.interceptor.WechatAuthority;
import co.fitstart.mobile.web.resolver.Secure;

/**
 * 
 * @author dan
 *
 */
@Controller
@RequestMapping(value = "/profile-offline-agent")
public class ProfileOfflineAgentController {
    
    @Autowired
    private UserAgentService userAgentService;
    
    @Autowired
    private UserService userService;
    
    /**
     * 
     * @param user
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @Router(targetUrl = "/required-wechat")
    @WechatAuthority
    @MobileDetect
    @WechatJsConfig
    public ModelAndView index(@Secure User user) {
        
        ModelAndView modelAndView = new ModelAndView("/profile/offline-agent/profile-agent");
        
        Student student = userService.getStudent(user);
        modelAndView.addObject("student", student);
        
        List<OfflineAgent> offlineAgents = userAgentService.listOfflineAgent(student);
        modelAndView.addObject("offlineAgents", offlineAgents);
        
        int courseAmount = 0;
        for(OfflineAgent offlineAgent : offlineAgents) {
            courseAmount = courseAmount + offlineAgent.getCourseAmount();
        }
        modelAndView.addObject("courseAmount", courseAmount);
        
        modelAndView.addObject("total", offlineAgents.size());
        
        return modelAndView;
    }

}
